{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Setting specifications for the graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "global graph_opts1 bgcolor(white) graphregion(color(white)) legend(region(lc(none) fc(none))) ylab(,angle(0) nogrid) subtitle(, justification(left) color(black) span pos(11)) title(, color(black) span)\n",
    "global pct `\" 0 \"0%\" .25 \"25%\" .5 \"50%\" .75 \"75%\" 1 \"100%\" \"'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Uploading data (.dta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "use \"https://github.com/SaoriIwa/Stata-IE-Visual-Library/raw/master/Library/Bar%20plots/Stack%20bar%20graph%20by%20two%20variables/data.dta\", clear"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Creating the graphs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qui graph bar med_b2_antister_cat?? ///\n",
    "\tif dr_3 == 1 ///\n",
    "\t, stack over(checkgroup) nofill ///\n",
    "\tylab($pct) legend(order(5 \"No Medication\" 4 \"Antibiotic and Steroid\" 3 \"Antibiotic\" 2 \"Steroid\" 1 \"No Antibiotic or Steroid\") ///\n",
    "\t\tc(1) symxsize(small) symysize(small) pos(3) size(small)) ///\n",
    "\t$graph_opts1 bar(5, color(white) lc(black) lp(solid) lw(thin)) ///\n",
    "\tbar(1,lw(thin) lc(black)) bar(2,lw(thin) lc(black)) bar(3,lw(thin) lc(black)) bar(4,lw(thin) lc(black)) ///\n",
    "\tsubtitle(\"Referral\" ,color(black) justification(center) pos(12)) \n",
    "\n",
    "qui graph save \"figure_1.gph\" , replace\n",
    "\n",
    "\n",
    "qui graph bar med_b2_antister_cat?? ///\n",
    "\tif dr_3 == 0 ///\n",
    "\t, stack over(checkgroup) nofill ///\n",
    "\tylab($pct) legend(order(5 \"No Medication\" 4 \"Antibiotic and Steroid\" 3 \"Antibiotic\" 2 \"Steroid\" 1 \"No Antibiotic or Steroid\") ///\n",
    "\t\tc(1) symxsize(small) symysize(small) pos(3) size(small)) ///\n",
    "\t$graph_opts1 bar(5, color(white) lc(black) lp(solid) lw(thin)) ///\n",
    "\tbar(1,lw(thin) lc(black)) bar(2,lw(thin) lc(black)) bar(3,lw(thin) lc(black)) bar(4,lw(thin) lc(black)) ///\n",
    "\tsubtitle(\"No Referral\" ,color(black) justification(center) pos(12)) \n",
    "\n",
    "qui graph save \"figure_2.gph\" , replace"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. Combining the graphs into one graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      ">  \n",
      "\n",
      ". qui graph save \"figure_2.gph\" , replace\n",
      "\n",
      ". . `67b90dea41d136fea1f4f982a7bce170'\n"
     ]
    }
   ],
   "source": [
    "grc1leg ///\n",
    "\t\"figure_2.gph\" ///\n",
    "\t\"figure_1.gph\" ///\n",
    "\t, pos(3) graphregion(color(white)) xsize(7) rows(1) leg(\"figure_2.gph\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. Exporting the graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "qui graph export \"figure.png\" , replace"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
